<template>
    <div class="change-Password">
        <p>修改密码</p>
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" 
            class="demo-ruleForm">
            <el-form-item label="原密码" prop="oldPassword" label-width="80px">
                <el-input v-model.trim="ruleForm.oldPassword" type="password" autocomplete="off"  show-password/>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword" label-width="80px" >
                <el-input v-model.trim="ruleForm.newPassword" type="password" autocomplete="off" show-password/>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword" label-width="80px">
                <el-input v-model.trim="ruleForm.confirmPassword" type="password" autocomplete="off" show-password/>
            </el-form-item>
            <el-form-item>
                <div class="submitForm">
                    <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>
<script lang="ts" setup>
import { reactive, ref} from 'vue'
import { post } from "@/utils/http";
import cookie from "js-cookie";
import { ElMessage } from 'element-plus'
import router from "@/router";
import type { FormInstance } from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const oldPassword = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入原密码'))
    }
    callback()
}

const newPassword = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入新密码'))
    } else if (value.length < 4) {
        callback(new Error('密码长度不小于4位'))
    }
    callback()
}
const confirmPassword = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请再次输入密码'))
    } else if (value !== ruleForm.newPassword) {
        callback(new Error("两次密码不一样"))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    oldPassword: '',
    newPassword: '',
    confirmPassword: '',
})

const rules = reactive({
    oldPassword: [{ validator: oldPassword, trigger: 'blur' }],
    newPassword: [{ validator: newPassword, trigger: 'blur' }],
    confirmPassword: [{ validator: confirmPassword, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            let from = {
                _gp:'admin',
                _mt:'newPassword',
                ADMINTOKEN:cookie.get('token'),
                oldPassword:ruleForm.oldPassword,
                newPassword:ruleForm.newPassword,
            }
            post(from).then(res=>{
                console.log(res)
                 if(200==res.errno){
                    ElMessage.success('修改成功')
                    router.replace("/login")
                 }else{
                    ElMessage.error(res.errmsg)
                 }
            })
        } else {
            console.log('error submit!')
            return false
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style lang="less" scoped>
.change-Password {
    width: 40%;
    margin: auto;
    padding: 10px 20px;
    border: 1px solid #ccc;

    p {
        margin-bottom: 10px;
        font-size: 22px;
        font-weight: bold;
        text-align: center;
    }

    /deep/ .el-input__inner {
        height: 40px;

    }
    .submitForm{
        width: 100%;
        text-align: center;
    }
}
</style>